In [19]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import BollingerBands, RSI
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.classifiers.morningstar import Sector
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
from alphalens.tears import create_returns_tear_sheet
from alphalens.tears import create_information_tear_sheet
In [8]:
def make_pipeline():
    base_universe = QTradableStocksUS()
    rsi = RSI(inputs=[USEquityPricing.close],
        window_length=14,
        mask=base_universe)
    bb = BollingerBands(inputs=[USEquityPricing.close],
        window_length=14,
        mask=base_universe,
        k=2)
    
    factor_to_analyze = rsi+bb.upper-bb.lower
    sector = Sector()

    return Pipeline(
        columns={'factor_to_analyze': factor_to_analyze, 'sector': sector},
        screen=QTradableStocksUS() & factor_to_analyze.notnull() & sector.notnull()
    )

factor_data = run_pipeline(make_pipeline(), '2015-1-1', '2016-1-1')
pricing_data = get_pricing(factor_data.index.levels[1], '2015-1-1', '2016-6-1', fields='open_price')
    
Pipeline Execution Time: 19.72 Seconds
In [16]:
longest_look_forward_period = 63 # week = 5, month = 21, quarter = 63, year = 252
range_step = 5

merged_data = get_clean_factor_and_forward_returns(
    factor=factor_data['factor_to_analyze'],
    prices=pricing_data,
    periods=range(1, longest_look_forward_period, range_step)
)

create_full_tear_sheet(merged_data)
Dropped 1.2% entries from factor data: 1.2% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).
max_loss is 35.0%, not exceeded: OK!
Quantiles Statistics
min max mean std count count %
factor_quantile
1 0.523861 61.376002 32.918540 8.705112 108917 20.018196
2 24.168581 70.568271 45.315583 7.329289 108773 19.991729
3 29.956318 77.859739 53.517135 7.627969 108762 19.989708
4 35.701547 86.838455 62.358119 8.075100 108773 19.991729
5 44.833972 409.268523 79.538751 16.024546 108865 20.008638
Returns Analysis
1D 6D 11D 16D 21D 26D 31D 36D 41D 46D 51D 56D 61D
Ann. alpha 0.017 0.018 0.002 -0.015 -0.022 -0.021 -0.015 -0.007 0.000 -0.001 -0.005 -0.006 -0.006
beta -0.088 -0.169 -0.155 -0.172 -0.194 -0.211 -0.196 -0.176 -0.139 -0.127 -0.131 -0.126 -0.113
Mean Period Wise Return Top Quantile (bps) 1.444 1.558 0.774 0.320 0.122 0.241 0.510 0.679 0.650 0.386 0.181 0.027 -0.023
Mean Period Wise Return Bottom Quantile (bps) -0.534 -2.847 -1.735 -0.945 -0.864 -1.120 -1.288 -1.505 -1.671 -1.590 -1.422 -1.310 -1.135
Mean Period Wise Spread (bps) 1.978 4.418 2.521 1.278 1.005 1.377 1.820 2.219 2.356 2.014 1.646 1.380 1.153
<matplotlib.figure.Figure at 0x7f41cfb2c080>
Information Analysis
1D 6D 11D 16D 21D 26D 31D 36D 41D 46D 51D 56D 61D
IC Mean 0.010 0.023 0.019 0.013 0.008 0.011 0.013 0.017 0.020 0.019 0.018 0.018 0.017
IC Std. 0.121 0.127 0.135 0.134 0.125 0.126 0.127 0.122 0.112 0.108 0.111 0.115 0.112
Risk-Adjusted IC 0.080 0.178 0.140 0.095 0.066 0.087 0.105 0.141 0.177 0.175 0.162 0.154 0.154
t-stat(IC) 1.268 2.830 2.233 1.506 1.043 1.387 1.674 2.241 2.819 2.786 2.577 2.445 2.447
p-value(IC) 0.206 0.005 0.026 0.133 0.298 0.167 0.095 0.026 0.005 0.006 0.011 0.015 0.015
IC Skew 0.046 -0.070 -0.087 -0.089 0.074 -0.007 0.047 0.095 0.338 0.511 0.453 0.295 0.081
IC Kurtosis -0.189 0.150 -0.431 -0.230 -0.167 -0.715 -0.547 -0.541 -0.300 -0.124 -0.327 -0.207 -0.077
Turnover Analysis
11D 16D 1D 21D 26D 31D 36D 41D 46D 51D 56D 61D 6D
Quantile 1 Mean Turnover 0.702 0.772 0.224 0.781 0.784 0.778 0.764 0.757 0.762 0.773 0.773 0.770 0.529
Quantile 2 Mean Turnover 0.781 0.796 0.458 0.797 0.800 0.801 0.802 0.799 0.799 0.802 0.801 0.801 0.725
Quantile 3 Mean Turnover 0.793 0.798 0.497 0.799 0.798 0.796 0.798 0.799 0.802 0.800 0.801 0.797 0.750
Quantile 4 Mean Turnover 0.784 0.797 0.432 0.797 0.801 0.800 0.796 0.795 0.797 0.802 0.802 0.801 0.722
Quantile 5 Mean Turnover 0.703 0.762 0.194 0.762 0.759 0.756 0.756 0.754 0.756 0.755 0.748 0.746 0.511
1D 6D 11D 16D 21D 26D 31D 36D 41D 46D 51D 56D 61D
Mean Factor Rank Autocorrelation 0.91 0.538 0.213 0.074 0.065 0.064 0.069 0.089 0.107 0.107 0.096 0.095 0.098
In [ ]:
 
In [ ]: